package com.kgc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.dto.ModelDto;

import com.kgc.mapper.ModelMapper;
import com.kgc.pojo.Model;
import com.kgc.req.ModelReq;
import com.kgc.service.ModelService;
import com.kgc.utils.oss.OssConfig;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品型号 服务实现类
 * </p>
 *
 * @author zsy
 * @since 2021-08-04
 */
@Service
public class ModelServiceImpl extends ServiceImpl<ModelMapper, Model> implements ModelService {

    @Autowired
    private ModelMapper modelMapper;
    @Autowired
    private OssConfig ossConfig;

    /**
     * 分页条件查询，先给页数在判断条件是否为空不为空则进行拼接，为空则不执行，然后使用分页插件分页，然后查询
     * @param modelReq
     * @return
     */
    @Override
    public Map<String, Object> search(ModelReq modelReq) {

        Map<String, Object> map = new HashMap<>();

        Integer pageSize = modelReq.getPageSize();
        Integer pageNo = modelReq.getPageNo();
        String modelName = modelReq.getModelName();
        Long brandId = modelReq.getBrandId();
        Long classifyId = modelReq.getClassifyId();

        //判读查询页的基本条件
        if (pageNo == null){
            pageNo = 1;
        }
        if (pageSize == null){
            pageSize = 5;
        }
        //开启分页
        Page<ModelDto> page = new Page<>(pageNo, pageSize);
        //判断查询条件是否为空不为空进行拼接
        QueryWrapper<Model> wrapper = new QueryWrapper<>();
        if(modelName != null){
            wrapper.eq("modelName", modelName);
        }
        if(brandId != null){
            wrapper.eq("brandId", brandId);
        }
        if(classifyId != null){
            wrapper.eq("classifyId", classifyId);
        }
        //拼接完成后进行查询
        List<ModelDto> list = modelMapper.search(page,wrapper);
        this.formatModelList(list);
        //将要返回的数据封装到map集合
        long current = page.getCurrent();
        long total = page.getTotal();
        long size = page.getSize();
        long pages = page.getPages();

        map.put("list", list);
        map.put("current", current);
        map.put("size", size);
        map.put("pages", pages);
        map.put("total", total);
        return map;
    }

    @Override
    public ModelDto getModelDetalById(Long id) {
        Model model = this.getById(id);
        ModelDto modelDto = new ModelDto(model);
        this.formatModel(modelDto);
        return modelDto;
    }


    //用于替换图片的地址
    public  void formatModelList(List<ModelDto> list) {
        if (list != null) {
            for (ModelDto modelDto : list) {
                this.formatModel(modelDto);
            }
        }
    }

    //给地址进行拼接
    public  void formatModel(ModelDto modelDto) {
        String ossWebUrl = ossConfig.getOssWebUrl();
        modelDto.setFaceImg(ossWebUrl + modelDto.getFaceImg());
        modelDto.setContentImg(ossWebUrl + modelDto.getContentImg());
    }
}
